import { defineConfig } from "vite";
import { loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import IconsResolver from "unplugin-icons/resolver";
import Icons from "unplugin-icons/vite";
import ViteRestart from "vite-plugin-restart";
import Inspect from "vite-plugin-inspect";
import viteCompression from "vite-plugin-compression";
import path from "node:path";
import optimizer from "vite-plugin-optimizer";
import UnoCSS from "unocss/vite";
import { UndrawUiResolver } from "undraw-ui/es/resolvers";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    extensions: [".js", ".ts", ".tsx", ".jsx", ".vue"],
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ["vue", "vue-router", "pinia"],
        dts: "types/auto-imports.d.ts",
        resolvers: [
          ElementPlusResolver(),
          IconsResolver({
            prefix: "Icon",
          }),
        ],
      }),
      Components({
        dts: "types/components.d.ts",
        resolvers: [
          IconsResolver({
            enabledCollections: ["ep"],
          }),
          ElementPlusResolver({
            importStyle: true,
          }),
          UndrawUiResolver,
        ],
        dirs: ["src/views/", "src/components/"],
        extensions: ["vue"],
      }),
      UnoCSS(),
      Icons({
        autoInstall: true,
      }),
      ViteRestart({
        restart: ["*.config.[jt]s", "**/config/*.[jt]s"],
      }),
      Inspect(),
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz",
      }),
      optimizer({
        child_process: () => ({
          find: /^(node:)?child_process$/,
          code: `const child_process = import.meta.glob('child_process');export {child_process as default}`,
        }),
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
        // "@/components/*": ["./src/components/*", "./src/components/*/index.vue"],
        // "/@/components/((?!.*[.](ts|js|tsx|jsx|vue)$).*$)/": path.resolve(
        //   __dirname,
        //   "./src/components/$1/index.vue"
        // ),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    server: {
      proxy: {
        "/api": {
          target: env.VITE_APP_API_BASE_URL,
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
      port: 5173,
      open: false,
      https: false,
      host: "0.0.0.0",
    },
    css: {
      preprocessorOptions: {
        scss: {
          modifyVars: {},
          javascriptEnabled: true,
          additionalData: '@use "@/assets/style/variable.scss" as *;',
        },
      },
    },
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
  };
});
